﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Renamer
{
    public partial class MainForm : Form
    {
        string[] file;
        public MainForm()
        {
            InitializeComponent();
        }

        private void ui_btn_selector_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择文件";
            fileDialog.Filter = "所有文件(*.*)|*.*"; ;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                file = fileDialog.FileNames;
                for (int i = 0; i < file.Length; i++ )
                {
                    // MessageBox.Show("已选择文件:" + file[i], "选择文件提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FileInfo fi = new FileInfo(file[i]);
                    // ui_textbox_src.AppendText("创建时间：" + fi.CreationTime.ToString() + "\n");
                    ui_textbox_src.AppendText(fi.Name + "\n");
                    // fi.MoveTo(file[i]+".txt");

                }
                StringBuilder sb = new StringBuilder("");
                sb.Append(ui_textbox_des.Text);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
        }

        // ---- 判断字符是否是数字或者是大小写字母
        private bool is_num_char (char ch)
        {
            return (ch >= '0' && ch <= '9')
                || (ch >= 'a' && ch <= 'z')
                || (ch >= 'A' && ch <= 'Z')
                ? true
                : false;
        }

        private void ui_button_commit_Click(object sender, EventArgs e)
        {
            // ---- 检验文件List的时间顺序是否合法，不合法不予运行
            DateTime[] file_date_time = new DateTime[file.Length];
            for (int i = 0; i < file.Length; i++)
            {
                FileInfo tmp_file_info = new FileInfo(file[i]);
                file_date_time[i] = tmp_file_info.CreationTime;
            }
            bool is_ok = true;
            for (int i = 0; i < file_date_time.Length - 1; i++)
            {
                if (file_date_time[i].ToFileTimeUtc() - file_date_time[i + 1].ToFileTimeUtc() > 0)
                {
                    is_ok = false;
                }
            }
            if (!is_ok)
            {
                // ---- 文件顺序不是按照时间排列的
                MessageBox.Show("文件选择需要按时间排序!");
                return;
            }
            // ---- 好吧，使用朴素原则，自己敲代码处理字符串吧。
            string[] des_file_name = new string[1000];
            int index = -1;
            string des_text = ui_textbox_des.Text;
            bool pre_is_num_char = false;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < des_text.Length; i++)
            {
                char ch = des_text[i];
                if (is_num_char(ch))
                {
                    if (!pre_is_num_char)
                    {
                        sb = new StringBuilder();
                    }
                    sb.Append(ch);
                    pre_is_num_char = true;
                }
                else
                {
                    if (pre_is_num_char)
                    {
                        index++;
                        des_file_name[index] = Convert.ToString(sb);
                    }
                    pre_is_num_char = false;
                }
            }

          
                // ---- 判断file是否为空呢
                if (file == null)
                {
                    MessageBox.Show("文件列表不能为空!");
                    return;
                }
            // ---- 判断src和des是否长度相等，长度不相等怎么修改文件名呢？            
            if (file.Length != index + 1)
            {
                MessageBox.Show("文件长度和订单数不相等","老子不干了");
                return;
            }

            for (int i = 0; i <= index; i++)
            {
                bool has_same = false;
                int same_count = 0;
                for (int j = i+1; j <= index; j++)
                {

                    //if (des_file_name[i] == des_file_name[j])
                    if (des_file_name[i].CompareTo(des_file_name[j]) == 0)
                    {
                        has_same = true;
                        same_count++;
                        des_file_name[j] = des_file_name[j] + "-" + Convert.ToString(same_count+1);
                    }
                }
                if (has_same)
                {
                    des_file_name[i] = des_file_name[i] + "-" + Convert.ToString(1);
                }
            }

                // ---- 检测目标文件名是否会出现重名，如果重名需要添加后缀来处理

                for (int i = 0; i <= index; i++)
                {
                    FileInfo fi = new FileInfo(file[i]);
                    fi.MoveTo(fi.DirectoryName + @"\" + des_file_name[i] + ".JPG");
                }
                MessageBox.Show("文件重命名完成!","一共有" + Convert.ToString(file.Length) + "个");
        }
    }
}

